
<template>
  <div id="back">
    <span id="title">物流管理系统</span>
  <div id="form">
    <div id="inner">
    <span>管理员登录</span>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="账号" prop="number">
        <el-input v-model="ruleForm.number" autocomplete="off"></el-input>
      </el-form-item>
  
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="check">
        <el-input type="password" v-model="ruleForm.check" autocomplete="off"></el-input>
        <span v-html="captcha" @click="getPic()"></span>
      </el-form-item>
  
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
  </div>
</div>
  
</template>
  
<script>
import axios from 'axios'
import userJson from '../data/user.json'
export default {
  data() {
    
    var validateNumber = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('账号不能为空'))
      }else{
        callback()
      }
    }

    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      }else{
        callback()
      }
    }

    var validateCheck = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      }else{
        callback()
      }
    }
    return {
      ma:'',
      captcha:'',
      ruleForm: {
        number: '',
        pass: '',
        check: ''
      },
      rules: {
        pass: [{ validator: validatePass, trigger: 'blur' }],
        check: [{ validator: validateCheck, trigger: 'blur' }],
        number: [{ validator: validateNumber, trigger: 'blur' }],
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
          this.checkUser(this.ruleForm.number,this.ruleForm.pass,this.ruleForm.check,userJson,this.ma);
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },



    
    checkUser(number,pass,check,userJson,code){
      this.$router.push('/about')
      axios.post("http://localhost:3000/check",{
        number:number,
        pass:pass,
        check:check,
        userJson:userJson,
        code:code
      }).then(res => {
        console.log(res);
        if(res.data.code === 200){
          this.$message({
            message: 'sueecss',
            type:'success',
          });
          
          console.log('success');
        }else if(res.data.code === 300)
          {
            this.$message({
            message: '验证码错误',
            type:'warning'
          });
          this.getPic();
        }else if(res.data.code === 0){
          this.$message({
            message: '用户名和密码错误',
            type:'warning'
          });
        }else if(res.data.code === 100){
          this.$message({
            message: '用户名错误',
            type:'warning'
          });
        }else if(res.data.code === 400){
          this.$message({
            message: '密码错误',
            type:'warning'
          });
        }
        
      })

    },
    getPic() {
      axios.get("http://localhost:3000/captcha")
        .then(res => {
        if (res.status == 200) {
          this.captcha=res.data.img;
          this.ma=res.data.text;
        }
      })
    },
    
  },
  mounted() {
    this.getPic();
  }
}
</script>
  
<style>
#form{
  width: 400px;
  height: 100%;
  float: right;
  background-color: aliceblue;
  padding-top: 20px;
}
#back{
  background: url(../images/background.jpg);
  width: 100%;
  height: 100%;
  position: absolute;
  background-size: cover;
  top: 0;
  left: 0;
}
#inner{
  margin-top: 100px;
}
#title{
  font-size: 40px;
  color:white;
  display: block;
  margin-top: 250px;
  margin-left: 30px;
  float: left;
}

</style>
  
